package com.iwhalecloud.bss.kite.cucc.web.service.accept.handler.scene;

import com.iwhalecloud.bss.kite.service.context.ServiceContext;
import org.springframework.stereotype.Service;

import com.iwhalecloud.bss.kite.client.vo.scene.OfferInstModuleVO;
import com.iwhalecloud.bss.kite.client.vo.scene.SceneVO;
import com.iwhalecloud.bss.kite.common.util.KiteListUtils;
import com.iwhalecloud.bss.kite.common.util.KiteObjectUtils;
import com.iwhalecloud.bss.kite.cucc.client.vo.scene.SceneNewOfferSeneVO;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.web.service.accept.bo.AcceptContext;
import com.iwhalecloud.bss.kite.web.service.accept.handler.scene.AbstractSceneHandler;
import lombok.RequiredArgsConstructor;

/**
 * @author : xie.zhengjun
 * @className : BatchAcceptModSceneHandler
 * @description : 批量受理单个修改页面
 * @date : 2021/6/7 16:51
 */
@Service
@RequiredArgsConstructor
public class BatchAcceptModSceneHandler extends AbstractSceneHandler {

    private final IQuerySceneInstService querySceneInstService;
    @Override
    public SceneVO initSceneVO(AcceptContext context) {
        return new SceneNewOfferSeneVO();
    }

    @Override
    public void execute(AcceptContext context) {
        String offerInstId = querySceneInstService.getExtParameterValue(context.getAcceptRequest().getSceneInstId(), "curOfferInstId");
        ServiceContext.setCurOfferInstId(offerInstId);
        context.getAcceptRequest().setOfferInstId(offerInstId);
        context.getAcceptRequest().setCurOfferInstId(offerInstId);
    }

    @Override
    public void afterExecute(AcceptContext context) {
        OfferInstModuleVO offerInstModuleVO = new OfferInstModuleVO();
        if (!KiteObjectUtils.isEmpty(context) && !KiteObjectUtils.isEmpty(context.getSceneVo())) {
            SceneNewOfferSeneVO sceneNewOfferSeneVO = (SceneNewOfferSeneVO) context.getSceneVo();
            if (!KiteObjectUtils.isEmpty(sceneNewOfferSeneVO) && KiteObjectUtils
                .isEmpty(sceneNewOfferSeneVO.getSceneProductModuleVO())) {
                offerInstModuleVO = sceneNewOfferSeneVO.getSceneProductModuleVO().getOffer();
            }
        }
        if (!KiteObjectUtils.isEmpty(offerInstModuleVO) && KiteListUtils
            .isNotEmpty(offerInstModuleVO.getOfferInstList())) {
            offerInstModuleVO.getOfferInstList().get(0).setOfferMember(
                genOfferInst(((SceneNewOfferSeneVO) context.getSceneVo()).getSceneProductModuleVO().getProduct()));
        }
    }

}
